package com.yuanfeng.filter;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.yuanfeng.pojo.Users;
import org.apache.struts2.ServletActionContext;
import javax.servlet.http.HttpSession;
/**
 * 登陆验证拦截器
 *
 * @author AdminTH
 */
public class AuthInterceptor extends AbstractInterceptor implements Action {

    public String intercept(ActionInvocation invocation) throws Exception {
        System.out.println("权限过滤");
        // 获取session中登陆用户
        HttpSession session = ServletActionContext.getRequest().getSession();
        Users user = (Users) session.getAttribute("user");
        // 如果登陆， 就放行（去到下一个拦截器、Action）
        if (user != null) {
            System.out.println(user.getName());
            System.out.println("放行");
            return invocation.invoke();
        }
        // 如果没有登陆，就不放行，跳转到“input”登陆视图
        return "input";
    }
    @Override
    public String execute() throws Exception {
        return null;
    }
}
